Page({

  /**
   * 页面的初始数据
   */
  leftAnimationNum:0,
  rightAnimationNum: 0,
  leftAnimationId:0,
  rightAnimationId:0,
  innerAudioContext:null,
  data: {
    actionSheetHidden: true,
    actionSheetItems: [],
    configs:[],
    currentConfig:{},
    leftAnimationData:{},
    rightAnimationData:{},
    leftCutNum:0,
    rightCutNum: 0
  },
  listenerButton: function () {
    this.leftStop();
    this.rightStop();
    this.stopAudio();
    this.setData({
      //取反
      actionSheetHidden: !this.data.actionSheetHidden
    });
  },
  listenerActionSheet: function (event) {
    this.leftStop();
    this.rightStop();
    this.stopAudio();
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    });
  },
  actionSheetChange: function (event) {
    var configs = wx.getStorageSync('configs');
    var currentConfig={};
    for (var i = 0; i < configs.length; i++) {
      if (configs[i].id==event.currentTarget.id) {
          currentConfig = configs[i];
          currentConfig.desc = currentConfig.desc.replace(/@/g, currentConfig.time + "秒")
      }
    }
    this.leftStop();
    this.rightStop();
    this.stopAudio();
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden,
      currentConfig: currentConfig,
      leftCutNum: currentConfig.time,
      rightCutNum: currentConfig.time
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  initASheet:function(){
    var configs = wx.getStorageSync('configs');
    var actionSheetItems=[];
    var isFirst=true;
    var currentConfig={};
    for (var i = 0; i < configs.length;i++){
      if (configs[i].checked){
        if (isFirst){
          currentConfig = configs[i];
          currentConfig.desc = currentConfig.desc.replace(/@/g, currentConfig.time+"秒")
          isFirst=false;
        }
        actionSheetItems.push(configs[i]);
      }
    }
    this.setData({ actionSheetItems: actionSheetItems,
     currentConfig:currentConfig,
     leftCutNum: currentConfig.time,
     rightCutNum: currentConfig.time
     });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initASheet();
    this.initAudio();
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  squareAnswer:function(event){
    this.leftStart();
  },
  counterAnswer: function (event) {
    console.log(event)
    this.rightStart();
  },
  leftStop:function(){
    var me = this;
    clearInterval(me.leftAnimationId);
    me.leftAnimationId=0;
  },
  leftStart: function () {
    var me = this;
    me.rightStop();
    if (me.leftAnimationId!=0){
      me.leftStop();
      me.stopAudio();
      return;
    }
    var leftCutNum = this.data.leftCutNum;
    if (leftCutNum<=0){
      return;
    }
    var leftAnimationNum = me.leftAnimationNum+45;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    }).rotate(leftAnimationNum).step();
    me.leftAnimationNum = leftAnimationNum;
    me.setData({ leftAnimationData: animation.export(),leftCutNum: leftCutNum-1 });
    var leftAnimationId = setInterval(function () {
      leftAnimationNum = me.leftAnimationNum + 45;
      leftCutNum = me.data.leftCutNum;
      if (leftCutNum<=0){
        me.leftStop();
        me.stopAudio();
        return;
      }
      if (leftCutNum <= me.data.currentConfig.countDown) {
        me.playAudio();
      }
      animation.rotate(leftAnimationNum).step();
      me.setData({
        leftAnimationData: animation.export(),
        leftCutNum: leftCutNum - 1
      })
      me.leftAnimationNum = leftAnimationNum;
    }.bind(this), 1000);
    me.leftAnimationId = leftAnimationId
   },
  rightStop: function () {
    var me = this;
    clearInterval(me.rightAnimationId);
    me.rightAnimationId = 0;
  },
  rightStart:function(){
    var me = this;
    me.leftStop();
    if (me.rightAnimationId != 0) {
      me.rightStop();
      me.stopAudio();
      return;
    }
    var rightCutNum = this.data.rightCutNum;
    if (rightCutNum <= me.data.currentConfig.countDown) {
      me.playAudio();
      return;
    }
    var rightAnimationNum = me.rightAnimationNum + 45;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    }).rotate(rightAnimationNum).step();
    me.rightAnimationNum = rightAnimationNum;
    me.setData({ rightAnimationData: animation.export(), rightCutNum: rightCutNum - 1 });
    var rightAnimationId = setInterval(function () {
      rightAnimationNum = me.rightAnimationNum + 45;
      rightCutNum = me.data.rightCutNum;
      if (rightCutNum <= 0) {
        me.rightStop();
        me.stopAudio();
        return;
      }
      if (rightCutNum <= me.data.currentConfig.countDown) {
        me.playAudio();
      }
      animation.rotate(rightAnimationNum).step();
      me.setData({
        rightAnimationData: animation.export(),
        rightCutNum: rightCutNum - 1
      })
      me.rightAnimationNum = rightAnimationNum;
    }.bind(this), 1000);
    me.rightAnimationId = rightAnimationId
  },
  initAudio:function(){
    const innerAudioContext = wx.createInnerAudioContext();
    innerAudioContext.autoplay = false;
    innerAudioContext.src = '/assets/audio/1792.mp3';
    innerAudioContext.loop=true;
    innerAudioContext.volume=1;
    innerAudioContext.onError((res) => {
      console.log(res.errMsg);
      console.log(res.errCode);
    });
    this.innerAudioContext = innerAudioContext;
  },
  playAudio:function(){
    if (this.innerAudioContext.paused){
      this.innerAudioContext.play();
    }
  },
  stopAudio:function(){
    if (!this.innerAudioContext.paused) {
       this.innerAudioContext.stop();
    }
  }
})